package com.m1905hd.user.web

import com.m1905hd.common.util.JSONUtil
import com.m1905hd.user.entity.Resource
import org.apache.commons.lang.StringUtils

/**
 *
 * @author weiwei.huang
 * @email ihuangweiwei@163.com
 * @date 15/4/3
 */
class PKUtil {

    static int safe2Int(String str, int defaultValue) {
        if (StringUtils.isBlank(str)) {
            return defaultValue
        }
        try {
            return Integer.valueOf(str)
        } catch (e) {
            e.printStackTrace()
        }
        return defaultValue
    }

    static def buildTree(List<Resource> resourceList) {

        def child_1 = getChild(0, resourceList)
        def list1 = new ArrayList()
        def root = [text: "root", id: "0", expanded: true, children: list1]

        child_1.each {
            def child2 = getChild(it.id, resourceList)
            def list2 = new ArrayList()
            def info = [text: it.name, id: it.id + "", expanded: true, url: it.url, permission: it.permission, position: it.position, type: it.type, children: list2]
            list1.add(info)

            child2.each {
                def child3 = getChild(it.id, resourceList)
                def list3 = new ArrayList()
                def info3 = [text: it.name, id: it.id + "", expanded: true, url: it.url, permission: it.permission, position: it.position, type: it.type, children: list3]
                list2.add(info3)


                child3.each {
                    def child4 = getChild(it.id, resourceList)

                    def list4 = new ArrayList()
                    def info34 = [text: it.name, id: it.id + "", expanded: true, url: it.url, permission: it.permission, position: it.position, type: it.type, children: list4]
                    list3.add(info34)

                    child4.each {
                        def child5 = getChild(it.id, resourceList)

                        def list5 = new ArrayList()
                        def info5 = [text: it.name, id: it.id + "", expanded: true, url: it.url, permission: it.permission, position: it.position, type: it.type]
                        list4.add(info5)
                    }
                }
            }
        }


        println(JSONUtil.beanToJson(root))
        return list1
    }

    static def buildTree(List<Resource> resourceList, List<Integer> ids) {

        def child_1 = getChild(0, resourceList)
        def list1 = new ArrayList()
        def root = [text: "root", id: "0", expanded: true, children: list1]

        child_1.each {
            def child2 = getChild(it.id, resourceList)
            def list2 = new ArrayList()
            def info = [text: it.name, id: it.id + "", expanded: true, url: it.url, permission: it.permission, position: it.position, type: it.type, children: list2]
            list1.add(info)

            child2.each {
                def child3 = getChild(it.id, resourceList)
                def list3 = new ArrayList()
                def info3 = [text: it.name, id: it.id + "", expanded: true, url: it.url, permission: it.permission, position: it.position, type: it.type, children: list3]
                list2.add(info3)

                child3.each {
                    def child4 = getChild(it.id, resourceList)

                    def list4 = new ArrayList()
                    def info34 = [text: it.name, id: it.id + "", expanded: true, url: it.url, permission: it.permission, position: it.position, type: it.type, children: list4]
                    list3.add(info34)
                    if (ids?.contains(it.id)) {
                        info34.checked = true
                    }
                    child4.each {
                        def child5 = getChild(it.id, resourceList)
                        def list5 = new ArrayList()
                        def info5 = [text: it.name, id: it.id + "", expanded: true, url: it.url, permission: it.permission, position: it.position, type: it.type]
                        list4.add(info5)
                        if (ids?.contains(it.id)) {
                            info5.checked = true
                        }
                    }
                }
            }
        }
        return list1
    }

    static List<Resource> getChild(int id, List<Resource> resourceList) {
        def children = new ArrayList()
        resourceList.each {
            if (it.parentId == id) {
                children.add(it)
            }
        }
        resourceList.removeAll(children)
        return children
    }


    public static List<Integer> buildIds(String ids) {
        def newIds = new ArrayList<Integer>()
        ids.split(",").each {
            newIds.add(Integer.valueOf(it))
        }
        return newIds
    }
}
